package cn.hzking.gjmall.activitys.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.login.LoginActivity;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.utils.DataCleanManager;
import cn.hzking.gjmall.utils.SPUtils;
import cn.hzking.gjmall.utils.UIUtils;

public class SettingActivity extends ColorStatuBarActivity {
    @Bind(R.id.rl_clean) RelativeLayout cleanRl;
    @Bind(R.id.rl_back) RelativeLayout backRl;
    @Bind(R.id.rl_about) RelativeLayout aboutRl;
    @Bind(R.id.rl_check) RelativeLayout checkRl;
    @Bind(R.id.rl_exit) RelativeLayout exitRl;
    @Bind(R.id.tv_cachesize) TextView cachesizeTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        try {
            //得到当前缓存大小
            String totalCacheSize = DataCleanManager.getTotalCacheSize(SettingActivity.this);
            cachesizeTv.setText(totalCacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        backRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        exitRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clear(true);
                Intent intent = new Intent(SettingActivity.this,LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
        cleanRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clear(false);
                cachesizeTv.setText("0KB");
                UIUtils.showToastShort(SettingActivity.this,"缓存清理完毕");
            }
        });
    }

    private void clear(boolean sp) {
        try {
            //清除SharedPreferences
            if(sp){
                SPUtils.clear(SettingActivity.this);
            }
            //清除缓存
            DataCleanManager.clearAllCache(SettingActivity.this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
